<script setup>
import ip from '@/utils/baseURL.js';

import { ref, computed, onMounted } from 'vue'
import { User, Calendar, Trophy, Message, Link, Aim, InfoFilled, UserFilled } from '@element-plus/icons-vue'
import axios from 'axios'

// 选中的年级
const selectedGrade = ref('all')

// 学生数据
const students = ref([])

// 获取所有年级并排序
const sortedGrades = computed(() => {
    const grades = [...new Set(students.value.map(student => parseInt(student.grade)))]
    return grades.sort((a, b) => parseInt(b) - parseInt(a))
})

// 获取学生信息
const getStudentInfo = async () => {
    try {
        const response = await axios.get(ip + '/inter/userInfo/getInfo')
        if (response.data.code === 200) {
             // 处理返回的数据并按年级降序排序
             students.value = response.data.data.records
                .map(student => {
                    // 处理加入时间，确保精确到日
                    const attendTime = student.attendTime
                    const formattedTime = attendTime
                        ? `${attendTime[0] || '--'}-${String(attendTime[1] || '--').padStart(2, '0')}-${String(attendTime[2] || '--').padStart(2, '0')}`
                        : '未记录时间'
                    return {
                        id: student.id,
                        name: student.name,
                        grade: String(student.grade), 
                        major: student.specialty,
                        position: student.position,
                        achievements: student.achievementsNum,
                        avatar: student.picture,
                        joinTime: formattedTime,
                        email: student.email,
                        blog: student.blog,
                        direction: student.direction,
                        briInfo: student.briInfo,
                        sex: student.sex
                    }
                })
                .sort((a, b) => b.grade - a.grade)
        }
    } catch (error) {
        console.error('获取学生信息失败:', error)
    }
}



// 按年级分组
const groupedStudents = computed(() => {
    const groups = {}
    students.value.forEach(student => {
        // 确保年级作为字符串处理
        const gradeKey = String(student.grade)
        if (!groups[gradeKey]) {
            groups[gradeKey] = []
        }
        groups[gradeKey].push(student)
    })

    // 转换为数组并排序
    let groupedArray = Object.entries(groups).map(([grade, students]) => ({
        grade,
        students
    })).sort((a, b) => parseInt(b.grade) - parseInt(a.grade))

     // 处理年级筛选
     if (selectedGrade.value !== 'all') {
        const filtered = groupedArray.filter(item => 
            item.grade === String(selectedGrade.value) // 确保类型一致
        )
        return filtered.length > 0 ? filtered : []
    }


    return groupedArray
})



// 页面加载时获取数据
onMounted(() => {
    getStudentInfo()
})
</script>

<template>
    <div class="students-container">
        <div class="main-content">
            <!-- 页面标题 -->
            <div class="page-header">
                <h1>实验室成员</h1>
            </div>

            <!-- 年级筛选 -->
            <div class="grade-filter">
                <el-radio-group v-model="selectedGrade" size="large">
                    <el-radio-button label="all">全部年级</el-radio-button>
                    <el-radio-button v-for="grade in sortedGrades" :key="grade" :label="grade">{{ grade }}级</el-radio-button>
                </el-radio-group>
            </div>


            <!-- 学生列表 -->
            <div class="students-list">
    <div v-for="group in groupedStudents" :key="group.grade" class="grade-section">
        <h2 class="grade-title">{{ group.grade }}级</h2>
        <el-row :gutter="30">
            <el-col 
                :xs="24" :sm="12" :md="8" :lg="6" 
                    v-for="student in group.students" :key="student.id">
                            <el-card class="student-card" :body-style="{ padding: '0px' }">
                                <div class="student-image">
                                     <!-- 使用 el-avatar 组件 -->
                                <el-avatar 
                                    :size="100" 
                                    :src="student.avatar" 
                                    class="student-avatar"
                                >
                                    <template #default>
                                        <div class="student-default-avatar">
                                            {{ student.name.slice(0, 1) }}
                                        </div>
                                    </template>
                                </el-avatar>
                                </div>
                                <div class="student-info">
                                    <h3 class="student-name">{{ student.name }}</h3>
                                    <div class="student-major">{{ student.major }}</div>
                                    <div class="position-gender">
                                        <div class="student-position">{{ student.position }}</div>
                                        <div class="student-gender">
                                            <el-icon><UserFilled /></el-icon>
                                            <span>{{ student.sex === 1 ? '男' : student.sex === 0 ? '女' : '性别未知' }}</span>
                                        </div>
                                    </div>
                                    <div class="student-join-time">
                                        <el-icon><Calendar /></el-icon>
                                        <span>加入时间：{{ student.joinTime }}</span>
                                    </div>
                                    <div class="student-details">
                                        <div class="details-grid">
                                            
                                            <div class="detail-item" v-if="student.blog">
                                                <el-icon><Link /></el-icon>
                                                <a :href="student.blog" target="_blank" class="blog-link">个人博客</a>
                                            </div>
                                            <div class="detail-item" v-if="student.direction">
                                                <el-icon><Aim /></el-icon>
                                                <span>{{ student.direction }}</span>
                                            </div>
                                            
                                        </div>
                                        <div class="detail-item bri-info1" >
                                                <el-icon><Trophy /></el-icon>
                                                <span>成果数量：{{ student.achievements }}</span>
                                            </div>
                                        <div class="detail-item bri-info1" v-if="student.email">
                                                <el-icon><Message /></el-icon>
                                                <span>{{ student.email }}</span>
                                            </div>
                                        <div v-if="student.briInfo" class="detail-item bri-info2">
                                            <el-icon><InfoFilled /></el-icon>
                                            <span>{{ student.briInfo }}</span>
                                        </div>
                                    </div>
                                </div>
                            </el-card>
                        </el-col>
                    </el-row>
                </div>
            </div>
        </div>
    </div>
</template>



<style lang="scss" scoped>
.students-container {
    width: 100%;
    overflow-x: auto;

    .main-content {
        width: 85%;
        margin: 0 auto;
        padding: 40px 20px;
        box-sizing: border-box;
    }

    .page-header {
        margin-bottom: 30px;
        text-align: center;

        h1 {
            font-size: 36px;
            color: #333;
            margin: 0;
            position: relative;
            display: inline-block;
            padding-bottom: 15px;

            &::after {
                content: '';
                position: absolute;
                bottom: 0;
                left: 50%;
                transform: translateX(-50%);
                width: 60px;
                height: 4px;
                background-color: #86B943;
            }
        }
    }

    .grade-filter {
        margin-bottom: 30px;
        text-align: center;

        :deep(.el-radio-button__inner) {
            padding: 12px 20px;
            font-size: 14px;
        }

        :deep(.el-radio-button__original-radio:checked + .el-radio-button__inner) {
            background-color: #86B943;
            border-color: #86B943;
        }
    }

    .students-list {
        .grade-section {
            margin-bottom: 40px;

            .grade-title {
                font-size: 24px;
                color: #333;
                margin: 0 0 20px 0;
                padding-bottom: 10px;
                border-bottom: 2px solid #86B943;
            }

            .el-row {
                width: 100%;
                display: flex;
                flex-wrap: wrap !important;  // 允许换行
                margin: 0 !important;
                justify-content: flex-start;  // 从左开始排列
            }

            .el-col {
                flex: 0 0 calc(25% - 30px);  // 固定宽度，每行4个
                width: calc(25% - 30px) !important;
                margin: 0 15px 30px;  // 添加底部间距
                max-width: calc(25% - 30px);  // 限制最大宽度
                
                // 覆盖响应式类
                &.el-col-24, &.el-col-sm-12, &.el-col-md-8, &.el-col-lg-6 {
                    width: calc(25% - 30px) !important;
                    max-width: calc(25% - 30px) !important;
                }
            }

            .student-card {
                margin-bottom: 0;
                transition: all 0.3s ease;
                overflow: hidden;
                height: 370px;  // 添加固定高度

                &:hover {
                    transform: translateY(-5px);
                    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
                }

                .student-image {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    width: 100%;
                    height: 140px;
                    margin-bottom: -20px;

                    .student-avatar {
                        width: 100px;
                        height: 100px;
                        object-fit: cover;
                        transition: transform 0.3s ease;

                        &:hover {
                            transform: scale(1.05);
                        }
                    }

                    .student-default-avatar {
                        display: flex;
                        justify-content: center;
                        align-items: center;
                        width: 100px;
                        height: 100px;
                        background: #f5f7fa;
                        color: #909399;
                        font-size: 30px;
                        border-radius: 50%;
                    }
                }

                .student-info {
                    padding: 15px;
                    text-align: center;

                    .student-name {
                        font-size: 18px;
                        color: #333;
                        margin: 0;
                        font-weight: bold;
                    }

                    .student-major {
                        font-size: 14px;
                        color: #666;
                        margin-bottom: 8px;
                    }

                    .position-gender {
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        gap: 20px;
                        margin-bottom: 8px;

                        .student-position {
                            font-size: 14px;
                            color: #86B943;
                            font-weight: 500;
                        }

                        .student-gender {
                            display: flex;
                            align-items: center;
                            gap: 4px;
                            font-size: 14px;
                            color: #666;

                            .el-icon {
                                color: #86B943;
                            }
                        }
                    }

                    .student-join-time {
                        display: flex;
                        align-items: center;
                        justify-content: center;
                        gap: 6px;
                        color: #666;
                        font-size: 12px;
                        margin-bottom: 12px;

                        .el-icon {
                            color: #86B943;
                        }
                    }

                    .student-details {
                        .details-grid {
                            display: grid;
                            grid-template-columns: repeat(2, 1fr);
                            gap: 8px;
                            margin-bottom: 12px;
                        }

                        .detail-item {
                            display: flex;
                            align-items: center;
                            gap: 6px;
                            color: #666;
                            font-size: 12px;
                            padding: 0 10px;
                            overflow: hidden;
                            text-overflow: ellipsis;
                            white-space: nowrap;
                            margin-left: 15px;

                            &.bri-info1 {
                                justify-content: center;
                                margin-top: 8px;
                                padding-top: 8px;
                            }
                            
                            &.bri-info2 {
                                justify-content: center;
                                margin-top: 8px;
                                padding-top: 8px;
                                border-top: 1px dashed #eee;
                            }

                            .el-icon {
                                color: #86B943;
                                flex-shrink: 0;
                            }

                            .blog-link {
                                color: #86B943;
                                text-decoration: none;
                                transition: color 0.3s ease;

                                &:hover {
                                    color: #6a9435;
                                    text-decoration: underline;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
</style>